cmake_minimum_required(VERSION 3.17)
SET(CMAKE_COLOR_MAKEFILE ON)
#POLICIES
CMAKE_POLICY(SET CMP0048 NEW)
CMAKE_POLICY(SET CMP0053 NEW)
CMAKE_POLICY(SET CMP0071 NEW)
CMAKE_POLICY(SET CMP0077 NEW)


set(APM_VERSION 1.0.0)
project(webrtc-audio-processing-ext VERSION ${APM_VERSION})
message(STATUS "Build WebRTC Audio Processing: ${APM_VERSION}")

#使能c++11
set(CMAKE_CXX_STANDARD 14)
if (NOT WIN32 AND ("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "AMD64" OR "${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "x86_64"))
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx2 -mfma -msse4.1")
endif()

#加载自定义模块
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(CheckIncludeFile)
include(CheckFunctionExists)
include(CheckLibraryExists)
include(CheckCSourceCompiles)
include(CMakeDetermineSystem)


#set(CMAKE_BUILD_TYPE "Release")
message(STATUS "编译类型:${CMAKE_BUILD_TYPE}")

#设置bin和lib库目录
set(RELEASE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/release)

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
    SET(LIBRARY_OUTPUT_PATH ${RELEASE_DIR}/linux/${CMAKE_BUILD_TYPE})
    SET(EXECUTABLE_OUTPUT_PATH ${RELEASE_DIR}/linux/${CMAKE_BUILD_TYPE})
    add_compile_options(-fPIC -Wall -Wno-unused-variable -Wno-unused-value)
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
    #    if (CMAKE_CL_64)
    #        set(CL_32_64 64)
    #    else ()
    #        set(CL_32_64 32)
    #    endif ()
    SET(LIBRARY_OUTPUT_PATH ${RELEASE_DIR}/windows${CL_32_64}/${CMAKE_BUILD_TYPE})
    SET(EXECUTABLE_OUTPUT_PATH ${RELEASE_DIR}/windows${CL_32_64}/${CMAKE_BUILD_TYPE})
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
    SET(LIBRARY_OUTPUT_PATH ${RELEASE_DIR}/mac/${CMAKE_BUILD_TYPE})
    SET(EXECUTABLE_OUTPUT_PATH ${RELEASE_DIR}/mac/${CMAKE_BUILD_TYPE})
    add_compile_options(-Wall -Wno-unused-variable -Wno-unused-value)
endif ()

include(FetchContent)

## Abseil-cpp 框架库依赖
set(BUILD_SHARED_LIBS OFF) # up to you
set(BUILD_TESTING OFF) # to disable abseil test
set(ABSL_ENABLE_INSTALL ON) # now you can enable install rules even in subproject...
FetchContent_Declare(
        absl
        GIT_REPOSITORY https://gitee.com/minhanghuang/abseil-cpp.git
        GIT_TAG 20210324.2 # 库版本
        SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/subprojects/abseil # 指定库下载地址
)
FetchContent_MakeAvailable(absl)

include_directories(webrtc)
LINK_DIRECTORIES(${LIBRARY_OUTPUT_PATH})

option(ENABLE_MSVC_MT "Enable MSVC Mt/Mtd lib" false)
option(CLANG_MSAN "Enable Clang memory sanitizer" OFF)
option(BUILD_LIB_STATIC "Build as static lib" ON)

set(LIB_TYPE SHARED)
if (${BUILD_LIB_STATIC})
    set(LIB_TYPE STATIC)
    message(STATUS "Build module as static lib")
    if (MSVC)
        set(CompilerFlags
                CMAKE_CXX_FLAGS
                CMAKE_CXX_FLAGS_DEBUG
                CMAKE_CXX_FLAGS_RELEASE
                CMAKE_C_FLAGS
                CMAKE_C_FLAGS_DEBUG
                CMAKE_C_FLAGS_RELEASE
                )
        foreach (CompilerFlag ${CompilerFlags})
            string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
        endforeach ()
    else (NOT ANDROID AND NOT IOS)
        set(CMAKE_SHARED_LINKER_FLAGS "-Wl-Bsymbolic")
    endif ()
else ()
    set(LIB_TYPE SHARED)
    message(STATUS "Build module as shared lib")
endif ()

set(subModules
        webrtc/api
        webrtc/audio
        webrtc/common_audio
        webrtc/modules
        webrtc/system_wrappers
        webrtc/rtc_base
        webrtc/third_party
        webrtc/test)

message("HostSystemName=${CMAKE_HOST_SYSTEM_NAME}")
message("HostSystemProcessor=${CMAKE_HOST_SYSTEM_PROCESSOR}")

foreach (mod ${subModules})
    add_subdirectory(${mod})
endforeach ()

set(ENABLE_TESTS ON)

if (ENABLE_TESTS)
    add_subdirectory(api)
endif ()